# -*- coding: utf-8 -*-
import scrapy
from scrapy.http import Request

import sys
sys.path.append("..")
from tutorial.items import VideoItem


class VideoSpider(scrapy.Spider):
    name = "video"
    allowed_domains = ["v.qq.com"]
    start_urls = [
        "http://v.qq.com/x/list/tv",
    ]
    url = "http://v.qq.com/x/list/tv"

    def parse(self, response):
        # filename = response.url.split("/")[-2]
        # with open(filename, 'wb') as f:
        #     f.write(response.body)

        # page_num = response.xpath(
        #     '//div/span/a[@class="page_num"]/text()').extract()[-1]
        # for ii in range(int(page_num)):
        for sel in response.xpath('//ul/li'):
            item = VideoItem()
            item['title'] = sel.xpath('div/strong/a/text()').extract()
            item['actor'] = sel.xpath('div/a/text()').extract()
            item['url'] = sel.xpath('div/strong/a/@href').extract()
            if item['title']:
                print(item['title'], item['actor'])
                yield item
                # with open('video', 'a') as f:
                #     f.writelines(str(item['title'][0]) + '\n')
                #     for people in item['actor']:
                #         f.writelines(str(people) + '  ')
                #     f.write('\n')

        nextlink = response.xpath(
            '//div/a[@class="page_next"]/@href').extract()
        if nextlink:
            link = nextlink[0]
            print("##################################")
            print(self.url + link)
            print("##################################")
            yield Request(self.url + link, callback=self.parse)

    def parse_item(self, response):
        for sel in response.xpath('//ul/li'):
            item = VideoItem()
            item['title'] = sel.xpath('div/strong/a/text()').extract()
            item['actor'] = sel.xpath('div/a/text()').extract()
            item['url'] = sel.xpath('div/strong/a/@href').extract()
            if item['title']:
                print(item['title'], item['actor'])
                yield item
